//
//  WBVistorView.swift
//  weibo
//
//  Created by GOD on 16/9/4.
//  Copyright © 2016年 磊王. All rights reserved.
//

import UIKit

class WBVistorView: UIView {
    //设置访客视图信息
    /**
     使用字典，设置访客视图消息
     
     - parameter dict: [imageName/message]
     */
    //如果是首页，imageName = null

    var vistorInfo:[String:String]?{
        didSet{
            guard let imageName = vistorInfo?["imageName"],
                let message = vistorInfo?["message"] else{
                    
                    return
                    
                    
            }
            //设置消息
            tipLabel.text = message
            
            
            //设置头像,首页不设置
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconView.image = UIImage(named: imageName)
            houseView.image = UIImage()
            maskIconView.hidden = true
            
        }
        
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /**
     旋转图标动画
     */
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        //动画完成不删除，如果界面被释放，动画自动被销毁
        anim.removedOnCompletion = false
        //将动画添加到图层
        
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
         //标志
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //首页房子
    private lazy var houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //label视图
    private lazy var tipLabel = UILabel.cz_labelWithText("关注一些人，回这里看看有什么惊喜",
                                                         fontSize: 14,
                                                         color: UIColor.darkGrayColor())
    //登陆按钮
    private lazy var loginButton = UIButton.cz_textButton("登陆",
                    fontSize: 16,
                    normalColor: UIColor.darkGrayColor(),
                    highlightedColor: UIColor.blackColor(),
                    backgroundImageName: "common_button_white_disable")
    
    //注册按钮
    
     private lazy var registerButton = UIButton.cz_textButton("注册",
                        fontSize: 16,
                        normalColor: UIColor.orangeColor(),
                        highlightedColor: UIColor.blackColor(),
                        backgroundImageName: "common_button_white_disable")
    
    
    
}

extension WBVistorView{
    func setUI(){
        backgroundColor = UIColor.cz_colorWithHex(0xEDEDED)
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        tipLabel.textAlignment = .Center
        
        //纯代码自动布局
        //取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //设置自动布局
        //icon的自动布局
        addConstraint(NSLayoutConstraint(item: iconView,
            attribute: .CenterX, //屏幕居中
            relatedBy: .Equal,
            toItem: self,//参照自己
            attribute: .CenterX,
            multiplier: 1,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView,
            attribute: .CenterY, //屏幕居中
            relatedBy: .Equal,
            toItem: self,//参照自己
            attribute: .CenterY,
            multiplier: 1,
            constant: -60))
        //房子视图
        addConstraint(NSLayoutConstraint(item: houseView,
            attribute: .CenterX, //屏幕居中
            relatedBy: .Equal,
            toItem: iconView,//参照自己
            attribute: .CenterX,
            multiplier: 1,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseView,
            attribute: .CenterY, //屏幕居中
            relatedBy: .Equal,
            toItem: iconView,
            attribute: .CenterY,
            multiplier: 1,
            constant: 0))
        //标签视图
        addConstraint(NSLayoutConstraint(item: tipLabel,
            attribute: .CenterX, //屏幕居中
            relatedBy: .Equal,
            toItem: iconView,//参照自己
            attribute: .CenterX,
            multiplier: 1,
            constant: 0))
       addConstraint(NSLayoutConstraint(item: tipLabel,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: iconView,
            attribute: .Bottom,
            multiplier: 1,
            constant: 20))
        //标签的宽度
        addConstraint(NSLayoutConstraint(item: tipLabel,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1,
            constant: 236))
        
        //注册的按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
            attribute: .Left,
            relatedBy: .Equal,
            toItem: tipLabel,
            attribute: .Left,
            multiplier: 1,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: tipLabel,
            attribute: .Bottom,
            multiplier: 1,
            constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1,
            constant: 100))
        
        //设置登陆按钮
        
        addConstraint(NSLayoutConstraint(item: loginButton,
            attribute: .Right,
            relatedBy: .Equal,
            toItem: tipLabel,
            attribute: .Right,
            multiplier: 1,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: tipLabel,
            attribute: .Bottom,
            multiplier: 1,
            constant: 20))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1,
            constant: 100))
        
        //设置遮罩
        //views用来定义VFL 中控件名称与实际映射关系
        let viewDict = ["maskIconView":maskIconView,
                        "registerButton" :registerButton]
        //metrics 在VFL中（）指定的常数映射关系
        let metrics = ["spaceing":-20]
        //水平方向
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
            "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: viewDict))
        //竖直方向
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|-0-[maskIconView]-(spaceing)-[registerButton]",
            options: [],
            metrics: metrics,
            views: viewDict))
        

        
        
        
        
        



    
    }

}